import router from '@/router'
import Nprogress from 'nprogress'
import getPageTitle from '@/utils/get-page-title'
import { getToken } from '@/utils/auth'
import { Message } from 'element-ui'

const whiteList = ['/login', '/404']

router.beforeEach((to, from, next) => {
  Nprogress.start()
  const token = getToken()
  if (token) {
    if (to.path === '/login') {
      next(false)
      Message.warning('已经登录')
      Nprogress.done()
    } else {
      next()
    }
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next({ path: '/login', query: { redirect: to.fullPath } })
      Message.warning('请您登录')
      Nprogress.done()

    }
  }
})

router.afterEach((to, from) => {
  Nprogress.done()
  document.title = getPageTitle(to.meta.title)
})
